
using UnityEngine;
using System.Collections;

public class shipMovement : MonoBehaviour 
{
	private float mouseX;
	private float mouseY;
	
	private Vector3 worldPos;

	public int MaxMovement;
	public string name;
	public AudioClip sound_selected;
	private AudioSource selected;
		
	public TurnAndPlayerVariables tpvScript;
	public AttackObjectBehavior attackObject;
	public PlayerStats playerStats;
	
	
	void Start()
	{
		tpvScript.AddShipToList(name);
		tpvScript.DeselectAll();
	}
	
	void OnCollisionEnter(Collision obj)
	{
		Debug.Log ("Collided");
		if (obj.gameObject.name == "Nebula")
		{
			playerStats.TerrainBonus = 10;
		}
	}
	
	void OnMouseOver()
	{
		if(Input.GetMouseButtonDown(0))
		{
			if (!tpvScript.AnySelected())
			{
				tpvScript.SetSelectedShip(name);
				attackObject.attacker = playerStats;
				AudioSource.PlayClipAtPoint(sound_selected,transform.position);
			}
			else if (tpvScript.AnyAttacking())
			{
				attackObject.defender = playerStats;
			}
		}
		
	}
	
	void Update()
	{		
		if (Input.GetMouseButtonDown(0) && tpvScript.IsShipSelected(name))
		{
			mouseX = Input.mousePosition.x;
			mouseY = Input.mousePosition.y;
			
			worldPos = Camera.main.ScreenToWorldPoint(new Vector3(mouseX, mouseY, 10));
			
			transform.position = worldPos;
			
			RaycastHit info;
			if (Physics.Raycast(transform.position, Vector3.forward, out info))
			{
				if (info.transform.name.Contains("Nebula"))
					playerStats.TerrainBonus = 10;
				else
					playerStats.TerrainBonus = 0;
			}
		}
			
		if (tpvScript.IsShipSelected(name) && Input.GetMouseButtonDown(1))
		{
			tpvScript.DeselectShip(name);
			selected.Stop();
			audio.Stop();
		}
	}
}